<?php

/**
 * Thinkphp默认分页样式转Bootstrap分页样式
 * @author H.W.H
 * @param string $page_html tp默认输出的分页html代码
 * @return string 新的分页html代码
 */
function bootstrap_page_style($page_html){
    if ($page_html) {
        // $page_show = str_replace('<div>','<nav><ul class="pagination">',$page_html);
        // $page_show = str_replace('</div>','</ul></nav>',$page_show);
        $page_show = str_replace('<div>','',$page_html);
        $page_show = str_replace('</div>','',$page_show);
        $page_show = str_replace('<span class="current">','<li class="active"><a>',$page_show);
        $page_show = str_replace('</span>','</a></li>',$page_show);
        $page_show = str_replace(array('<a class="num"','<a class="prev"','<a class="next"','<a class="end"','<a class="first"'),'<li><a',$page_show);
        $page_show = str_replace('</a>','</a></li>',$page_show);
    }
    return $page_show;
}

function bootstrap_page_style2($page_html){
    if ($page_html) {
        // $page_show = str_replace('<div>','<nav><ul class="pagination">',$page_html);
        // $page_show = str_replace('</div>','</ul></nav>',$page_show);
        $page_show = str_replace('<div>','',$page_html);
        $page_show = str_replace('</div>','',$page_show);
        //$page_show = str_replace('<span class="current">','<li class="active"><a>',$page_show);
        //$page_show = str_replace('</span>','</a></li>',$page_show);
        $page_show = str_replace(array('<a class="num"','<a class="prev"','<a class="next"','<a class="end"','<a class="first"'),'<li><a',$page_show);
        $page_show = str_replace('</a>','</a></li>',$page_show);
    }
    return $page_show;
}

function importExcel($filename, $exts = 'xls')
{
    // 导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
    vendor ("PHPExcel.PHPExcel");
    // 创建PHPExcel对象，注意，不能少了\
    $PHPExcel = new \PHPExcel();
    // 如果excel文件后缀名为.xls，导入这个类
    if ($exts == 'xls') {
        $PHPReader = new \PHPExcel_Reader_Excel5();
    } else 
        if ($exts == 'xlsx') {
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
    
    // 载入文件
    $PHPExcel = $PHPReader->load($filename);
    // 获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
    $currentSheet = $PHPExcel->getSheet(0);
    // 获取总列数
    $allColumn = $currentSheet->getHighestColumn();
    // 获取总行数
    $allRow = $currentSheet->getHighestRow();
    // 循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
    for ($currentRow = 1; $currentRow <= $allRow; $currentRow ++) {
        // 从哪列开始，A表示第一列
        for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn ++) {
            // 数据坐标
            $address = $currentColumn . $currentRow;
            // 读取到的数据，保存到数组$arr中
            $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
        }
    }
    return $data;
}

function exportExcel($fileName, $headArr, $data){
    vendor("PHPExcel.PHPExcel");
    $date = date("Y_m_d", time());
    $fileName .= "_{$date}.xls";
    $objPHPExcel = new \PHPExcel();
    $objProps = $objPHPExcel->getProperties();
    // 设置表头
    $key = ord("A");
    foreach ($headArr as $v) {
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
        $key += 1;
    }
    $column = 2;
    $objActSheet = $objPHPExcel->getActiveSheet();
    // print_r($data);exit;
    foreach ($data as $key => $rows) { // 行写入
        $span = ord("A");
        foreach ($rows as $keyName => $value) { // 列写入
            $j = chr($span);
            $objActSheet->setCellValue($j . $column, $value);
            $span ++;
        }
        $column ++;
    }
    
    $fileName = iconv("utf-8", "gb2312", $fileName);
    // 重命名表
    // 设置活动单指数到第一个表,所以Excel打开这是第一个表
    $objPHPExcel->setActiveSheetIndex(0);
    header('Content-Type: application/vnd.ms-excel');
    header("Content-Disposition: attachment;filename=\"$fileName\"");
    header('Cache-Control: max-age=0');
    
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output'); // 文件通过浏览器下载
    exit();
}